package cn.wh.controller;

import cn.wh.utils.FastDFSClient;
import cn.wh.utils.JSONResult;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Author: HongXiaoHan
 * @Description
 * @Date: 2021/2/8 21:01
 */

//文件上传控制器
@RestController
@RequestMapping("file")
public class FileController {


    @Autowired
    FastDFSClient fastDFSClient;

    @RequestMapping("/uploadImage")
    public JSONResult uploadImage(@RequestParam("file") MultipartFile file) {
        StorePath storePath;
        try {
            storePath = fastDFSClient.uploadFile(file);
        } catch (IOException e) {
            return JSONResult.errorMsg("上传失败");
        }
        return JSONResult.ok("/" + storePath.getFullPath());
    }

}
